const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const AION_PeiFang = db.collection('AION-PeiFang')

// 获取openId云函数入口函数
exports.main = async (event, context) => {

    var name = event.data.name
    var min = event.data.min
    var max = event.data.max
    var pinzhi = event.data.pinzhi
    var type = event.data.type

    var params = {}

    if (name)
        params.name = db.RegExp({
            regexp: String(name),
            options: 'i',
        })
    if (pinzhi)
        params.pinzhi = pinzhi
    if (type)
        params.type = type

    if (min || max)
        params.pNum = _.gte(Number(min)).and(_.lte(Number(max)))


    // return await AION_ZB_WQ.where(params).get()
    const res = await AION_PeiFang.where(params)
    const list = (await res
        .skip((event.data.pageIndex - 1) * event.data.pageSize)
        .limit(event.data.pageSize)
        .get()).data

    const total = (await res.count()).total

    return {
        list: list,
        total: total,
    }

};